package com.ty.zls.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ty.zls.R;
import com.ty.zls.bean.Account;
import com.ty.zls.bean.Constants;
import com.ty.zls.bean.Water;
import com.ty.zls.task.BaseTask;
import com.ty.zls.util.HttpUtil;
import com.ty.zls.util.StringUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckWaterAdapter extends RecyclerView.Adapter<CheckWaterAdapter.ViewHolder> {

    private int total;
    private List<Water> datas = new ArrayList<>();
    Context context;

    private int pageNum = 1;
    private int pageSize = 5;

    public CheckWaterAdapter(Context context) {
        super();
        this.context = context;
    }

    @NonNull
    @Override
    public CheckWaterAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        QMUILinearLayout root = (QMUILinearLayout) inflater.inflate(R.layout.check_water_item, viewGroup, false);
        root.setRadiusAndShadow(QMUIDisplayHelper.dp2px(context, 15),
                QMUIDisplayHelper.dp2px(context, 14), 0.25f);
        return new ViewHolder(root);
    }



    @Override
    public void onBindViewHolder(@NonNull CheckWaterAdapter.ViewHolder viewHolder, int i) {
        Water water = datas.get(i);
        viewHolder.setId(water.getAccountId());
        viewHolder.setTel(water.getTel());
        viewHolder.setName(water.getName());
        viewHolder.setCurScale(water.getScale());
        viewHolder.setLastScale(water.getLastScale());
        viewHolder.setTag(i);
    }



    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView water_id_tv;
        private TextView water_name_tv;
        private TextView water_tel_tv;
        private TextView water_lastscale_tv;
        private TextView water_curscale_tv;
        private QMUIRoundButton edit_waterRe;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            water_id_tv = itemView.findViewById(R.id.water_id_tv);
            water_name_tv = itemView.findViewById(R.id.water_name_tv);
            water_tel_tv = itemView.findViewById(R.id.water_tel_tv);
            water_lastscale_tv = itemView.findViewById(R.id.water_lastscale_tv);
            water_curscale_tv = itemView.findViewById(R.id.water_curscale_tv);
            edit_waterRe = itemView.findViewById(R.id.edit_waterRe);
            /* 排序号修改 */
            edit_waterRe.setOnClickListener(this);
        }

        public void setTag(int tag){
            edit_waterRe.setTag(tag);
        }

        public void setId(String id){
            water_id_tv.setText(id);
        }

        public void setName(String name){
            water_name_tv.setText(name);
        }

        public void setTel(String tel){
            water_tel_tv.setText(tel);
        }

        public void setLastScale(String scale){
            water_lastscale_tv.setText(scale);
        }

        public void setCurScale(String scale){
            water_curscale_tv.setText(scale);
        }

        @Override
        public void onClick(View v) {
            int tag = (int) v.getTag();
            final Water water = datas.get(tag);
            switch (v.getId()){
                case R.id.edit_waterRe:
                    final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(context);
                    builder.setTitle("修改本期读数")
                            .setPlaceholder("在此输入输入本期读数")
                            .setDefaultText(water.getScale())
                            .setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
                            .addAction("取消", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    dialog.dismiss();
                                }
                            })
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(final QMUIDialog dialog, int index) {
                                    final String text = builder.getEditText().getText().toString();
                                    if(StringUtils.isEmpty(text)){
                                        Toast.makeText(context,"请输入本期读数",Toast.LENGTH_SHORT).show();
                                    }else{
                                        if(text.equals(water.getScale())){
                                            dialog.dismiss();
                                            return;
                                        }
                                        if(Integer.valueOf(text).compareTo(Integer.valueOf(water.getLastScale())) < 0 ){
                                            Toast.makeText(context,"本期读数应大于等于上期读数",Toast.LENGTH_SHORT).show();
                                            return;
                                        }
                                        Map<String,String> map = new HashMap<>();
                                        map.put("sort",text);
                                        map.put("accountId",water.getAccountId());
                                        map.put("endScale",text);
                                        map.put("id",water.getId());
                                        new BaseTask(HttpUtil.EDIT_WATER, context, new BaseTask.SuccessCallback() {
                                            @Override
                                            public void excute(JSONObject result) {
                                                dialog.dismiss();
                                                water.setScale(text);
                                                notifyDataSetChanged();
                                                showTip();
                                            }
                                        }).execute(map);
                                    }
                                }
                            })
                            .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
                    break;
            }
        }
    }

    private QMUITipDialog tipDialog;
    private Handler handler = new Handler();
    private void showTip(){
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("修改成功")
                .create();
        tipDialog.show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        },1000);
    }

    public List<Water> getDatas() {
        return datas;
    }

    public void setDatas(List<Water> datas) {
        this.datas = datas;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}
